#include "Common.h"
#if defined (_DEBUG) && defined (DETECT_MEM_LEAKS)
	#define _CRTDBG_MAP_ALLOC
	#include <stdlib.h>
	#include <crtdbg.h>
#endif
#include "HndToRawFiles.h"
#include <windows.h>
#include <memory>
#include <string>
#include <vector>

#if defined (_DEBUG) //|| defined (__cplusplus)
	#pragma comment (lib, "C:\\AAAfiles\\Anthony\\ImageView\\Debug\\ImageViewd.lib")
#else
	#pragma comment (lib, "C:\\AAAfiles\\Anthony\\ImageView\\Release\\ImageView.lib")
#endif
//! Check the arguments to make sure they are correct
//! \return False if the provided arguments are invalid, true
//! if they are valid
bool checkArgs(int argc, char* argv[])
{
	if (argc != 9)
	{
		printf("About: This program will convert a batch of hnd files to 32 bit grayscale raw files.\n\n"
               "Usage: hnd_to_raw hnd_dir hnd_prefix hnd_postfix raw_dir raw_prefix raw_postfix start_index end_index\n"
			   "hnd_dir         - The directory of the hnd files\n"
			   "hnd_prefix      - The prefix of the hnd files\n"
			   "hnd_postfix     - The postfix of the hnd files\n"
			   "raw_dir         - The directory where the raw files will be created.\n"
			   "raw_prefix      - The prefix of the newly created raw files\n"
			   "raw_postfix     - The postfix of the raw files\n"
			   "start/end index - If the hnd_prefix is \"hnd_\", and the hnd_postfix is \"\", and the start_index "
			   "is \"0\", and the end_index is \"100\", all the files between hnd_0.hnd to hnd_100.hnd"
			   " will be converted.\n");
		return false;
	}

	return true;
}


int main(int argc, char* argv[]) 
{
#if defined (_DEBUG) && defined (DETECT_MEM_LEAKS)
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
	//_CrtSetBreakAlloc(149);
#endif
	if (!checkArgs(argc, argv))
		return 0;

	char* hndDir, * hndPrefix, * hndPostfix, * rawDir, * rawPrefix, * rawPostfix;
	unsigned int startIndex, endIndex;

	startIndex = atoi(argv[7]);
	endIndex   = atoi(argv[8]);

	hndDir     = argv[1];
	hndPrefix  = argv[2];
	hndPostfix = argv[3];
	rawDir     = argv[4];
	rawPrefix  = argv[5];
	rawPostfix = argv[6];

	HndToRawFiles(hndDir, hndPrefix, hndPostfix, rawDir,
		          rawPrefix, rawPostfix, startIndex, endIndex);

	const unsigned int numpairs = endIndex - startIndex + 1;
	if (_setmaxstdio((numpairs + 1) * 2 + 1) == -1)
		_asm int 3;

	std::vector<FILE*> hndFiles(numpairs);
	std::vector<FILE*> rawFiles(numpairs);

	memset(&hndFiles[0], 0, sizeof(FILE*) * numpairs);
	memset(&rawFiles[0], 0, sizeof(FILE*) * numpairs);

	
	for (uint i = (uint)startIndex; i <= (uint)endIndex; ++i)
	{
		char indexString[32];
		_itoa(i, indexString, 10);

		std::string hndfp(hndDir);
		hndfp += '\\';
		hndfp += hndPrefix;
		hndfp += indexString;
		hndfp += hndPostfix;
		hndfp += ".hnd";

		if (GetFileAttributes(hndfp.c_str()) == INVALID_FILE_ATTRIBUTES)
		{
			printf("Error: hnd_file (%s) does not exist.", hndfp.c_str());
			goto CleanUp;
		}

		std::string rawfp(rawDir);
		rawfp += '\\';
		rawfp += rawPrefix;
		rawfp += indexString;
		rawfp += rawPostfix;
		rawfp += ".raw";

		hndFiles[i] = fopen(hndfp.c_str(), "rb");
		if (!hndFiles[i])
			_asm int 3;
		rawFiles[i] = fopen(rawfp.c_str(), "wb");
		if (!rawFiles[i])
			_asm int 3;
	}

	//HndToRawFilesL(hndFiles, rawFiles);

CleanUp:
	for (unsigned int i = 0; i < numpairs; ++i)
	{
		if (hndFiles[i]) fclose(hndFiles[i]);
		if (rawFiles[i]) fclose(rawFiles[i]);
	}
	return 0;
}
